In [1]:
import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
sns.set(palette="hls", font_scale=1.5)
In [2]:
# make point with cumulative sum
points = np.random.randn(50).cumsum()
points
Out[2]:
In [3]:
# plt.plot(x, y): x, y = point(x, y) on coordinate
# put y only(default x = auto)
plt.plot(points)
plt.show()
In [4]:
# put x and y points
plt.plot(range(0, 250, 5), points)
plt.show()
In [5]:
# set color, marker, line
plt.plot(points, 'co:')
plt.show()
In [6]:
# style setting
plt.plot(points, 'co-', lw=3, ms=5, mfc='b') # lw=linewidth, ms=marker size, mfc=marker face color
plt.xlim(-10, 60) # set x axis limit
plt.ylim(-5, 5) # set y axis limit
plt.show()
In [7]:
# style setting
plt.plot(points, 'co-', lw=3, ms=5, mfc='b')
plt.xlim(-10, 60) # set x axis limit
plt.ylim(-5, 5) # set y axis limit
plt.xticks([0, 25, 50]) # set x axis ticks
plt.yticks([-7, -3, 1], [r'$\theta$', r'2$\theta$', r'3$\theta$']) # LaTeX input available
plt.grid(False) # grid off
plt.show()
In [8]:
# draw multiple lines
## plt.plot(x1, y1, xy1_style, x2, y2, xy2_style, x3, y3, xy3_style)
plt.plot(points, points, 'bo',
points, 2*points, 'cs-',
points, 0.5*points, 'r.', lw=0.5, ms=8)
plt.show()
In [9]:
# draw multiple lines -2
plt.plot(points, 'co-', lw=3, ms=5, mfc='b')
plt.plot(points*0.5)
plt.show()
In [10]:
# legend, title
plt.rc('font', family='nanumgothic') # set font family, use Korean
plt.plot(points, label='random points') # set plot 1 label
plt.plot(0.5 * points, label='임의값') # set plot 2 label
plt.legend()
plt.xlabel('random x') # set x label
plt.ylabel('random y') # set y label
plt.title('random plot') # set the title
plt.show()
In [11]:
plt.plot(points)
plt.annotate(# text, arrow point(x, y), xy coordinate
r'(text)', xy=(40, -4), xycoords='data',
# text location from text coordinate, text coordinate
xytext=(-50, 50), textcoords='offset points',
# font, arrow shape
fontsize=20, arrowprops=dict(arrowstyle="->", linewidth=3, color="b"))
plt.show()
In [12]:
plt.figure(figsize=(20, 3))
plt.plot(points)
plt.show()
In [13]:
ax1 = plt.subplot(2, 1, 1)
plt.plot(points)
ax2 = plt.subplot(2, 1, 2)
plt.plot(np.random.randn(50))
plt.show()
In [14]:
x = [3, 2, 1]
y = [1, 2, 3]
xlabel = ['한개', '두개', '세개']
# plt.bar: vertical / plt.barh: horizontal
plt.bar(x, y, align='center') # align: ceter(default), edge
plt.xticks(x, xlabel)
plt.show()
In [15]:
x = np.random.randint(0, 10, 10)
print(x)
arrays, bins, patches = plt.hist(x, bins=6)
plt.show()
In [16]:
# value counts for each bin
print(arrays)
# the range of each bin
print(bins)
In [17]:
plt.pie([30, 50, 10], # size
labels = ['피자', '햄버거', '감자튀김'], # label
colors = ['pink', 'salmon', 'tomato'], # colors
explode = (0.01, 0.01, 0.2), # explode
autopct = '%.2f%%', # set the ratio label format
shadow = True, # pie chart shadow
startangle = 0) # rotate the chart
plt.axis('equal') # chart shape slope
plt.title('품목별 매출비중')
plt.show()